<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use  think\Request;

class Student extends Base
{     
    
	
		
	public function Index(){
		
		 return view();
	}

	public function importdata(){
		$request = Request::instance();
		$data =$request->post('data');
		$de_json = json_decode($data,TRUE);
		$count_json = count($de_json);
		$mid= session("mid");
		for ($i = 0; $i < $count_json; $i++)
	    {
					
				    $s1["number"] = $de_json[$i]['number'];
					$s1["truename"] = $de_json[$i]['truename'];
					$s1["sex"] = $de_json[$i]['sex'];
					$s1["classid"] =$de_json[$i]['classid'];
					$s1["fathername"] = $de_json[$i]['fathername'];
					$s1["fatherphone"] = $de_json[$i]['fatherphone'];
					$s1["mothername"] = $de_json[$i]['mothername'];
					$s1["motherphone"] = $de_json[$i]['motherphone'];
					$s1["otherphone"] = $de_json[$i]['otherphone'];
					$s1["createtime"] =  date('Y-m-d H:i:s',time());
					$s1["mid"] = session("mid");

					db('student')->insertGetId($s1);
  
		}

		return ['status'=>200,'message'=>'导入成功'];

	}
	
	
	function Add(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["number"] =$request->post('number');
				$data["truename"] =$request->post('truename');
				$data["classid"] =$request->post('classid');
				$data["fathername"] =$request->post('fathername');
				$data["mothername"] =$request->post('mothername');
				$data["fatherphone"] =$request->post('fatherphone');
				$data["motherphone"] =$request->post('motherphone');
				$data["otherphone"] =$request->post('otherphone');
				$data["createtime"] = date('Y-m-d H:i:s',time());
				$data["imgurl"] =$request->post('imgurl');
				$data["sex"] =$request->post('sex');
				
			
				
			if($data["id"]){
				if(db('student')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				$student = db("student")->where("number='".$data["number"]."' and mid=".session("mid"))->find();
			
				if($student){
				   return ['status'=>500,'message'=>'学号重复了'];
				}else {
					$data["mid"] = session("mid");
					if(db('student')->insertGetId($data)){
						   return ['status'=>200,'message'=>'添加成功'];
						  
					  }else{
						  return ['status'=>500,'message'=>'修改失败'];
					  }

				}
				
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('student')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
	}
	 public function GetStudent(){
		$where="mid=".session("mid")."";
		$request = Request::instance();
		if($request->get('number')){
			$where=$where." and number like '%".$request->get('number')."%'";
		}
		if($request->get('truename')){
			$where=$where." and truename like '%".$request->get('truename')."%'";
		}
		if($request->get('classid')){
			$where=$where." and classid=".$request->get('classid');
		}
		if($request->get('state')){
			$where=$where." and state=".$request->get('state');
		}else{
			$where=$where."  and state=0";
		}


		$count =db('student')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('student')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {

			$studentandparent= db("studentandparent")->where("studentid=".$value["id"])->find();
			if($studentandparent){
				$value["isbind"]="是";
				$value["studentandparent_id"]=$studentandparent["id"];
			}else{
				$value["isbind"]="否";
			}

			$classinfo = db('myclass')->where("id=".$value["classid"])->find();
			$value["classname"] =$classinfo["classname"];
			if($value["videoetime"]!=""){
				$value["videoetime"]=date("Y-m-d",strtotime($value["videoetime"]));  
			}
			$id = $value["id"];

			if($value["fatherphone"]==""){
			//	$studentandparent = db("studentandparent")->where("studentid=".$id)->find();
				if($studentandparent){
					if($studentandparent["relation"]=="爸爸"){
						$userinfo = db("users")->where("id=".$studentandparent["userid"])->find();
						Db::execute('update student set fatherphone=? where id=?',[$userinfo["phone"],$id]);
						$value["fatherphone"]=$userinfo["phone"];
					}
					
				}

			}
			if($value["mothername"]==""){
				//$studentandparent = db("studentandparent")->where("studentid=".$id)->find();
				if($studentandparent){
					if($studentandparent["relation"]=="妈妈"){
						$userinfo = db("users")->where("id=".$studentandparent["userid"])->find();
						Db::execute('update student set motherphone=? where id=?',[$userinfo["phone"],$id]);
						$value["motherphone"]=$userinfo["phone"];
					}
				}

			}

			
		}
		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	 public function GetStudentList(){
		 $request = Request::instance();
		 
		 
		 $where="mid=".session("mid")." and state=0 ";
		 if($request->post('classid')){
			$where=$where." and classid=".$request->post('classid');
		 }
		 if($request->post("classlist")){
			 $where=$where." and classid in (".$request->post('classlist').")";
		 }


		$list = db('student')->where($where)->select();
	
		return json($list);
		
	}
	function Del(){
		$request = Request::instance();
		$id =$request->post('id');
		
			$where = 'id='.$id;
		   $list=db('student_card')->where("studentid=".$id)->delete();
		   $list=db('studentandparent')->where("studentid=".$id)->delete();
		   $list=db('student')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	public function Work(){
		
		 return view();
	}
	
	
	
	function WorkAdd(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["title"] =$request->post('title');
				$data["desc"] =$request->post('desc');
				$data["teacherid"] =$request->post('teacherid');
				$data["createtime"] = date('Y-m-d H:i:s',time());
				$data["overtime"] =$request->post('overtime');
				$data["cid"] =$request->post('cid');
			
			
		
			
				
			if($data["id"]){
				if(db('work')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				$data["mid"] = session("mid");
			
			  if(db('work')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('work')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
	}
	 public function GetWork(){
		$where="mid=".session("mid");
		$count =db('work')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('work')->where("")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			
			
		}
		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	
	function WorkDel(){
		$request = Request::instance();
		$id =$request->post('id');
		
			$where = 'id='.$id;
		  
		   $list=db('work')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	public function Food(){
		
		 return view();
	}
	
	
	
	function FoodAdd(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
				$data["title"] =$request->post('title');
				$data["imgurl"] =$request->post('imgurl');
				$data["desc"] =$request->post('desc');
				$data["day"] =$request->post('day');
				$data["type"] =$request->post('type');
			
				$data["createtime"] = date('Y-m-d H:i:s',time());
				
			
				
			if($data["id"]){
				if(db('food')->update($data)){
					
					  return ['status'=>200,'message'=>'修改成功'];
				}else{
					 return ['status'=>500,'message'=>'修改失败'];
				}
			}else{
				
				$data["mid"] = session("mid");
			  if(db('food')->insertGetId($data)){
					 return ['status'=>200,'message'=>'添加成功'];
					
				}else{
					return ['status'=>500,'message'=>'修改失败'];
				}
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('food')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
		
			
		
			
		}
		
		
	
	
		return view();
	}
	 public function GetFood(){
		$where="mid=".session("mid");

		$count =db('food')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('food')->where($where)->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			
			
		}
		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	
	function FoodDel(){
		$request = Request::instance();
		$id =$request->post('id');
		
			$where = 'id='.$id;
		  
		   $list=db('food')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}

	public function card(){
		$request = Request::instance();
		
		$id =$request->get('id');
		$this->assign('id',$id);

		return view();
   }
   public function getcard(){
		$request = Request::instance();
		
		$studentid =$request->post('studentid');
		$list = db('student_card')->where("studentid=".$studentid)->select();

		$obj["list"]=$list;
		$obj["status"]=200;
		return json($obj);
   }
   public function addcard(){
		$request = Request::instance();
		$data["id"] =$request->post('id');
		$data["cardnumber"] =$request->post('cardnumber');
		$data["desc"] =$request->post('desc');
		$data["studentid"] =$request->post('studentid');
		$data["mid"] = session("mid");
		$data["createtime"] = date('Y-m-d H:i:s',time());

		if($data["id"]){
			if(db('student_card')->update($data)){
				
				return ['status'=>200,'message'=>'修改成功'];
			}else{
				return ['status'=>500,'message'=>'修改失败'];
			}
		}else{
		
		if(db("student_card")->where("cardnumber='".$data["cardnumber"]."'")->find()){
			return ['status'=>500,'message'=>'卡号重复'];
		}

		if(db('student_card')->insertGetId($data)){
				return ['status'=>200,'message'=>'添加成功'];
				
			}else{
				return ['status'=>500,'message'=>'修改失败'];
			}
		}
   }
   function carddel(){
		$request = Request::instance();
		$id =$request->post('id');
		$where = 'id='.$id;
	  
	   $list=db('student_card')->where($where)->delete();
	   if($list!==false) {
		 return ['status'=>200,'message'=>'删除成功'];
	   }else{
		  return ['status'=>500,'message'=>'删除失败'];
	   }
	
	}
	function setnumber(){
		$studentid = $_POST["studentid"];
		$cardnumber = $_POST["cardnumber"];
		$obj = Db::execute('update student set number=? where id=?',[$cardnumber,$studentid]);
		if($obj!==false) {
			return ['status'=>200,'message'=>'设置成功'];
		  }else{
			 return ['status'=>500,'message'=>'设置失败'];
		  }
	}

	
	public function bind(){
		$request = Request::instance();
		$id =$request->get('id');
		$this->assign('id',$id);

		return view();
   }
   public function get_studentandparent(){

		$request = Request::instance();
		$studentid =$request->post('studentid');
		$list = db('studentandparent')->join("users","users.id=studentandparent.userid")->where("studentid=".$studentid)->field("studentandparent.*,users.nickname,users.phone")->select();
		$obj["list"]=$list;
		$obj["status"]=200;
		return json($obj);
	 }
	 
	 public function exportstudent(){
		  	$request = Request::instance();
				$number =$_GET["number"];
				$truename =$_GET["truename"];
				$classid =$_GET["classid"];
				$state =$_GET["state"];

				$where="student.mid=".session("mid");

				if($request->get('number')){
					$where=$where." and number like '%".$request->get('number')."%'";
				}
				if($request->get('truename')){
					$where=$where." and truename like '%".$request->get('truename')."%'";
				}
				if($request->get('classid')){
					$where=$where." and classid=".$request->get('classid');
				}
				if($request->get('state')){
					$where=$where." and student.state=".$request->get('state');
				}else{
					$where=$where."  and student.state=0";
				}
		
		
			
				 
				$list = db('student')->join("myclass","myclass.id=student.classid")->where($where)->order("student.id desc")->select();

			  vendor('PHPExcel.PHPExcel');
        //3.实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        //4.激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '学号')
                ->setCellValue('B1', '姓名')
                ->setCellValue('C1', '班级');
      
        //6.循环刚取出来的数组，将数据逐一添加到excel表格。
        for($i=0;$i<count($list);$i++){
            $objPHPExcel->getActiveSheet()->setCellValue('A'.($i+2),$list[$i]['number']);//ID
            $objPHPExcel->getActiveSheet()->setCellValue('C'.($i+2),$list[$i]['truename']);//标签码
            $objPHPExcel->getActiveSheet()->setCellValue('B'.($i+2),$list[$i]['classname']);//防伪码
        }
        //7.设置保存的Excel表格名称
        $filename = '学生信息'.date('ymd',time()).'.xls';
        //8.设置当前激活的sheet表格名称；
        $objPHPExcel->getActiveSheet()->setTitle('学生信息');
				//9.设置浏览器窗口下载表格
				ob_end_clean();
        header("Content-Type: application/force-download");  
        header("Content-Type: application/octet-stream");  
        header("Content-Type: application/download");  
        header('Content-Disposition:inline;filename="'.$filename.'"');  
        //生成excel文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //下载文件在浏览器窗口
        $objWriter->save('php://output');
	 }
}
